// pagesEnterprise/pages/housingManage/housingManage.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: '0', // 3 带参数返回上一页
    dataList: [],
    formData: {
      gardenId: '',
      search: '',
      pageSize: 10,
      pageNum: 1,
      total: 0,
    },
    isEdit: false,
    type: 0
  },
  /**
   * 重置事件
   */
  resetEvent() {
    this.setData({"formData.search" : ''})
    this.inputConfirm()
  },
  backEvent(event) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('backEvent', event.detail);
    wx.navigateBack()
  },
  inputChange(event) {
    this.data.formData.search = event.detail.value
  },
  inputConfirm() {
    this.data.formData.pageNum = 1;
    this.getRequestList()
  },
  async getRequestList() {
    const { formData, dataList } = this.data
    const res = await ajax.get('/my/house/assess/garden/list', formData).finally(() => wx.stopPullDownRefresh());
    this.data.formData.total = res.total
    res.rows.forEach(item => ((item.coverLabel = item.coverLabel?.split(',') || []), (item.videoPath ? item.hosingRentImages.unshift({ type: 'video', image: item.videoPath }) : null)))
    this.setData({ dataList: formData.pageNum == 1 ? res.rows : dataList.concat(res.rows) })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.title) wx.setNavigationBarTitle({ title: options.title })
    this.setData({ type: options.type || 0, status: options.status || '0', 'formData.gardenId': options.gardenId || '', isEdit: options.edit || false })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.inputConfirm()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      'formData.search': ''
    })
    this.data.formData.pageNum = 1;
    this.getRequestList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { formData, dataList } = this.data
    if (formData.total == dataList.length) return
    this.data.formData.pageNum += 1;
    this.getRequestList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})